﻿#include <alibabacloud/oss/OssClient.h>
#include "SimpleClient.h"
#include <fstream>
using namespace AlibabaCloud::OSS;
#include "OssOperate.h"

/* 初始化OSS账号信息 */
std::string AccessKeyId = "";
std::string AccessKeySecret = "";
std::string Endpoint = "oss-cn-beijing.aliyuncs.com";
std::string BucketName = "mindx-media";

void list()
{

	/* 初始化网络等资源 */
	InitializeSdk();

	ClientConfiguration conf;
	OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

	/* 列举文件 */
	ListObjectsRequest request(BucketName);
	auto outcome = client.ListObjects(request);

	if (!outcome.isSuccess()) {
		/* 异常处理 */
		std::cout << "ListObjects fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
	}
	else {
		for (const auto& object : outcome.result().ObjectSummarys()) {
			std::cout << "object" <<
				",name:" << object.Key() <<
				",size:" << object.Size() <<
				",last modified time:" << object.LastModified() << std::endl;
		}
	}

	/* 释放网络等资源 */
	ShutdownSdk();
}

int upload_mem()
{
	/* 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。*/
	std::string ObjectName = "exampleobject.txt";

	/* 初始化网络等资源。*/
	InitializeSdk();

	ClientConfiguration conf;
	OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
	std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
	*content << "Thank you for using Alibaba Cloud Object Storage Service!";
	PutObjectRequest request(BucketName, ObjectName, content);

	/*（可选）请参见如下示例设置访问权限ACL为私有（private）以及存储类型为标准存储（Standard）。*/
	//request.MetaData().addHeader("x-oss-object-acl", "private");
	//request.MetaData().addHeader("x-oss-storage-class", "Standard");

	/* 上传文件。*/
	auto outcome = client.PutObject(request);

	if (!outcome.isSuccess()) {
		/* 异常处理。*/
		std::cout << "PutObject fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return -1;
	}

	/* 释放网络等资源。*/
	ShutdownSdk();
	return 0;
}


int upload_file()
{

	/* 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。*/
	std::string ObjectName = "12.jpg";

	/* 初始化网络等资源。*/
	InitializeSdk();

	ClientConfiguration conf;
	OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
	/* 填写本地文件完整路径，例如D:\\localpath\\examplefile.txt，其中localpath为本地文件examplefile.txt所在本地路径。*/
	std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>("E:\\atlas\\opt_worksapce\\opt_test_pics\\200dk_grey\\12.jpg", std::ios::in | std::ios::binary);
	PutObjectRequest request(BucketName, ObjectName, content);

	/*（可选）请参见如下示例设置访问权限ACL为私有（private）以及存储类型为标准存储（Standard）。*/
	//request.MetaData().addHeader("x-oss-object-acl", "private");
	//request.MetaData().addHeader("x-oss-storage-class", "Standard");

	auto outcome = client.PutObject(request);

	if (!outcome.isSuccess()) {
		/* 异常处理。*/
		std::cout << "PutObject fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return -1;
	}

	/* 释放网络等资源。*/
	ShutdownSdk();
	return 0;
}


int download_file()
{
	/* 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。*/
	std::string ObjectName = "12.jpg";
	/* 下载Object到本地文件examplefile.txt，并保存到指定的本地路径中（D:\\localpath）。如果指定的本地文件存在会覆盖，不存在则新建。*/
	/* 如果未指定本地路径，则下载后的文件默认保存到示例程序所属项目对应本地路径中。*/
	std::string FileNametoSave = "F:\\12.jpg";

	/* 初始化网络等资源。*/
	InitializeSdk();

	ClientConfiguration conf;
	OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

	/* 下载Object到本地文件。*/
	GetObjectRequest request(BucketName, ObjectName);
	request.setResponseStreamFactory([=]() {return std::make_shared<std::fstream>(FileNametoSave, std::ios_base::out | std::ios_base::in | std::ios_base::trunc | std::ios_base::binary); });

	auto outcome = client.GetObject(request);

	if (outcome.isSuccess()) {
		std::cout << "GetObjectToFile success" << outcome.result().Metadata().ContentLength() << std::endl;
	}
	else {
		/* 异常处理。*/
		std::cout << "GetObjectToFile fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return -1;
	}

	/* 释放网络等资源。*/
	ShutdownSdk();
	return 0;
}


int download_mem()
{
	/*填写不包含Bucket名称在内的Object的完整路径，例如desrfolder/exampleobject.txt。*/
	std::string ObjectName = "12.jpg";

	/*初始化网络等资源。*/
	InitializeSdk();

	ClientConfiguration conf;
	OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

	/*获取文件到本地内存。*/
	GetObjectRequest request(BucketName, ObjectName);
	auto outcome = client.GetObject(request);
	if (outcome.isSuccess()) {
		std::cout << "getObjectToBuffer" << " success, Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl;
		/*通过read接口读取数据。*/
		auto& stream = outcome.result().Content();
		char buffer[256];
		while (stream->good()) {
			stream->read(buffer, 256);
			auto count = stream->gcount();
			/*根据实际情况处理数据。*/
		}
	}
	else {
		/*异常处理。*/
		std::cout << "getObjectToBuffer fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return -1;
	}

	/*释放网络等资源。*/
	ShutdownSdk();
	return 0;
}

int test1()
{
	upload_mem();
	upload_file();
	list();
	download_file();
	download_mem();
	return 0;
}


int test2()
{
	// 上传文件
	OssOperate::GetInstance().upload("test/123.jpg", "E:\\atlas\\opt_worksapce\\opt_test_pics\\200dk_grey\\12.jpg");
	// 下载文件
	OssOperate::GetInstance().download("test/123.jpg", "E:\\atlas\\opt_worksapce\\opt_test_pics\\200dk_grey\\12.jpg");
	// 列举文件
	OssOperate::GetInstance().listObjects();
	// 删除文件
	//OssOperate::GetInstance().delFile("test/123.jpg");
	return 0;
}

int main(void)
{
	test2();
	return 0;
}
